package cn.mst.vrs.dal.mysql.order;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.mst.vrs.controller.admin.order.vo.OrderPageReqVO;
import cn.mst.vrs.dal.dataobject.order.OrderDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.ChainWrappers;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

import static cn.mst.vrs.enums.ReservationOrderStatusEnum.*;

/**
 * 订单 Mapper
 *
 * @author 超级管理员
 */
@Mapper
public interface OrderMapper extends BaseMapperX<OrderDO> {

    default PageResult<OrderDO> selectPage(OrderPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<OrderDO>()
                .eqIfPresent(OrderDO::getTypeId, reqVO.getTypeId())
                .eqIfPresent(OrderDO::getUserId, reqVO.getUserId())
                .eqIfPresent(OrderDO::getItemId, reqVO.getItemId())
                .eqIfPresent(OrderDO::getCount, reqVO.getCount())
                .eqIfPresent(OrderDO::getTotal, reqVO.getTotal())
                .eqIfPresent(OrderDO::getExtendInfo, reqVO.getExtendInfo())
                .eqIfPresent(OrderDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(OrderDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(OrderDO::getId));
    }

    default List<OrderDO> currentOrder(Long userId) {
        return ChainWrappers.lambdaQueryChain(this)
                .eq(OrderDO::getUserId, userId)
                .in(OrderDO::getStatus, WAIT_PAID.getStatus(), WAIT_USED.getStatus(), USED.getStatus())
                .list();
    }

    default Page<OrderDO> historyOrder(Long userId, Page<OrderDO> page) {
        return ChainWrappers.lambdaQueryChain(this)
                .eq(OrderDO::getUserId, userId)
                .page(page);
    }

    default List<OrderDO> selectOrderNotExpired() {
        return ChainWrappers.lambdaQueryChain(this)
                .eq(OrderDO::getStatus, WAIT_USED.getStatus())
                .list();
    }
}